package cn.edu.zut.controller;

import cn.edu.zut.domin.BookLendInfo;
import cn.edu.zut.service.IBookLendInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/bookLendInfo3")
public class BookLendInfoController3 {
    /**
     * 展示所有图书
     * @param page
     * @param size
     * @return
     * @throws Exception
     */
    ClassPathXmlApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
    IBookLendInfoService bookLendInfoService =(IBookLendInfoService)ac.getBean("bookLendInfoServiceImpl");
    @RequestMapping("/findAll")
    public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                @RequestParam(name = "size", required = true, defaultValue = "4") int size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<BookLendInfo> bookLendInfos = bookLendInfoService.findAllBookLendInfo(page,size);
        PageInfo<BookLendInfo> pageInfo = new PageInfo<>(bookLendInfos);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("LentBookAndReaderInfo");
        return mv;
    }

    @RequestMapping("/save")
    public String save(BookLendInfo bookLendInfo) throws Exception {
        bookLendInfoService.saveBookLendInfo(bookLendInfo);
        return "redirect:findAll";
    }//save为jsp请求时的符号，网页上请求调用保存方法时
    //会调用保存函数

    @RequestMapping("/delete")
    public String delete(String bookId,String readerId) throws Exception {
        bookLendInfoService.deleteBookLendInfo(bookId,readerId);
        //删除完成后回到全部信息页面
        return "redirect:findAll";
    }
    //delete为jsp请求时的符号，网页上请求调用删除方法时
    //会调用删除函数


    @RequestMapping("/update")
    public String update(BookLendInfo bookLendInfo) throws Exception {
        bookLendInfoService.updateBookLendInfo(bookLendInfo);
        return "redirect:findAll";
    }
    //delete为jsp请求时的符号，网页上请求调用更新方法时
    //会调用更新函数


    @RequestMapping("/findOne")
    public ModelAndView findOne(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                @RequestParam(name = "size", required = true, defaultValue = "4") int size, String bookId, String readerId) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<BookLendInfo> bookLendInfos = bookLendInfoService.getBookLendInfoByBookIdAndReaderId(page,size,bookId,readerId);
        mv.addObject("bookLendInfoList", bookLendInfos);
        mv.setViewName("LentBookAndReaderInfo");
        return mv;
    }
    //查找的请求会通过controller，寻找方法
    //调用方法的符号就是findone

}
